package com.tianzeculture.stagebao.activity.mine;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tianzeculture.stagebao.R;

import java.util.List;

/**
 * Created by Administrator on 2018/9/20.
 */

public class ChooseMoneyAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

    private int index = -1;
    Activity context;

    public ChooseMoneyAdapter(@LayoutRes int layoutResId, @Nullable List<String> data, Activity context) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final String item) {

        final CheckBox checkBox = helper.getView(R.id.cboxMoney);
        checkBox.setText("¥ " + item);
        checkBox.setId(helper.getLayoutPosition());
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    if (index != -1) {
                        //找到上次点击的checkbox,并把它设置为false,对重新选择时可以将以前的关掉
                        CheckBox tempCheckBox = (CheckBox) context.findViewById(index);
                        if (tempCheckBox != null)
                            tempCheckBox.setChecked(false);
                    }
                    index = compoundButton.getId();//保存当前选中的checkbox的id值
                    listener.getMoney(item);
                }
            }
        });
        if (helper.getLayoutPosition() == index)//比对position和当前的temp是否一致
            checkBox.setChecked(true);
        else
            checkBox.setChecked(false);
    }


    public void setListener(OnMoneyListener listener) {
        this.listener = listener;
    }

    OnMoneyListener listener ;
   public interface OnMoneyListener {
        void getMoney(String money);
    }
}
